import 'package:manager/model/UserModel.dart';
import 'package:manager/util/sp_util.dart';

class UserUtil {
  static const String SP_USER_ID = "sp_user_id";
  static const String SP_USER_NAME = "sp_user_name";
  static const String SP_USER_PHONE = "sp_user_phone";
  static const String SP_USER_TOKEN = "sp_user_token";

  static const String SP_IS_LOGIN = "sp_is_login";

  //保存个人信息
  static User saveUserInfo(Map data) {
    if (data != null) {
      int id = data['id'];
      String name = data['username'];
      String phone = data['userphone'];
      String token = data['token'];

      SpUtil.putString(SP_USER_ID, id.toString());
      SpUtil.putString(SP_USER_PHONE, phone);
      SpUtil.putString(SP_USER_NAME, name);
      SpUtil.putString(SP_USER_TOKEN, token);

      SpUtil.putBool(SP_IS_LOGIN, true);

      User info = User(id: id.toString(), userName: name, userPhone: phone, token: token);
      return info;
    }
    return null;
  }

  //获取用户信息
  static User getUserInfo() {
    bool isLogin = SpUtil.getBool(SP_IS_LOGIN);
    if (isLogin == null || !isLogin) {
      return User();
    }
    User info = User();
    info.id = SpUtil.getString(SP_USER_ID);
    info.userName = SpUtil.getString(SP_USER_NAME);
    info.userPhone = SpUtil.getString(SP_USER_PHONE);
    info.token = SpUtil.getString(SP_USER_TOKEN);
    return info;
  }

  //判断用户是否登录
  static bool isLogin() {
    bool b = SpUtil.getBool(SP_IS_LOGIN);
    return b != null && b;
  }
}
